<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/8/27
 * Time: 13:57
 */
namespace Admin\Controller;

use Shop\Api\ProductApi;
use Shop\Api\ProductPackageApi;
use Shop\Api\ProductSkuApi;

class ProductPackageController extends AdminController{

    public function index(){

        //查询套餐
        $type = I('type',-1);
        $s = I('s');
        $map = [];
        if($type!=-1){
            $map['dt_type'] = $type;
        }
        if(!empty($s)){
            $this->assign('s',$s);
            $map['package_name'] = ['like', '%'.$s.'%'];
        }

        $page = ['curpage'=>1, 'size'=>10];
        $params = ['type'=>$type,'s'=>$s];
        $result = apiCall(ProductPackageApi::QUERY,[$map,$page,false,$params]);
        $this -> assign('show', $result['info']['show']);
        $this -> assign('list', $result['info']['list']);

        $this->display();
    }

    public function detail(){

        //套餐详情
        $package_id = I('id');
        if(empty($package_id))$this->error('参数缺失');

        $result = $this->getPackageInfo($package_id);
        if($result['status']){
            $this->assign('package_info',$result['info']);
        }else{
            $this->error($result['info']);
        }

        $this->display();
    }

    public function edit(){

        //套餐详情
        $package_id = I('id');
        if(!empty($package_id)){
            $result = $this->getPackageInfo($package_id);

            if($result['status']){
                $this->assign('package_info',$result['info']);
            }else{
                $this->error($result['info']);
            }
        }

        $this->display();

    }

    public function queryProduct(){
        if(IS_POST){

            $map = ['uid'=>UID];
            $store_id = 0;
            $result = apiCall("Shop/Store/getInfo",array($map));
            if($result['status']){
                $store_id = $result['info']['id'];
            }
            if(empty($store_id)){
                $this->ajaxReturn(['status'=>false,'info'=>'请先创建店铺!']);
            }

            $cate_id = I('post.cate_id');
            $curpage = I('post.curpage',1);
            $name = trim(I('post.name'));
            $dt_origin_country = I('post.dt_origin_country',0,'int');
            $size = I('post.size',10);
            $ids = I('post.ids');
            $normal_sp = I('post.normal_sp',1); //仅上架未删除商品
            $map = [
                'store_id' => $store_id
            ];
            if(boolval($normal_sp)){
                $map['onshelf'] = 1;
                $map['status'] = 1;
            }
            if(!empty($cate_id)){
                $map['cate_id'] = $cate_id;
            }
            if(!empty($name)){
                $map['name'] = ['like','%'.$name.'%'];
            }
            if(!empty($dt_origin_country)){
                $map['dt_origin_country'] = $dt_origin_country;
            }
            if(!empty($ids)){
                $map['id'] = ['in',$ids];
            }

            $page = ['curpage'=>$curpage,'size'=>$size];
            $result = apiCall(ProductApi::QUERY,[$map,$page]);
            if($result['status']){
                $list = $result['info']['list'];
                $count = $result['info']['count'];
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                foreach($list as &$val){
                    $val['img_id'] = $PICTURE_REMOTE_URL.$val['img_id'].'&size=120';
                }
                if(is_null($list)){
                    $list = [];
                }
                $this->ajaxReturn(['status'=>true,'info'=>['count'=>$count,'list'=>$list]]);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }


        }
    }

    /**
     * 查找商品SKU
     */
    public function querySku(){

        if(IS_POST){

            $pid = I('post.pid');
            $map = ['id'=> $pid];
            //查询商品名称
            $result = apiCall(ProductApi::GET_INFO,[$map]);
            if($result['status']){
                if(is_null($result['info']))$this->ajaxReturn(['status'=>false,'info'=>'该商品不存在']);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }
            $product_name = $result['info']['name'];


            $map = [
                'product_id' => $pid
            ];
            $result = apiCall(ProductSkuApi::QUERY_NO_PAGING,[$map]);
            if($result['status']){
                $list = $result['info'];
                if(is_null($list)){
                    $list = [];
                }
                foreach($list as &$val){
                    $val['product_name'] = $product_name;
                }
                $this->ajaxReturn(['status'=>true,'info'=>$list]);
            }else{
                $this->ajaxReturn(['status'=>false,'info'=>'error']);
            }


        }

    }

    /**
     * 删除套餐
     */
    public function delete(){
        if(IS_POST){

            $ids = I('post.ids');
            if(empty($ids))$this->error('参数缺失');

            $result = apiCall(ProductPackageApi::PACKAGE_DELETE,[$ids]);

            if($result['status']){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $id = I('get.id');

            if(empty($id))$this->error('参数缺失');

            $result = apiCall(ProductPackageApi::PACKAGE_DELETE,[[$id]]);

            if($result['status']){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
    }

    /**
     * 保存套餐
     */
    public function savePackage(){
        if(IS_POST){
            $package_info = I('post.package_info');

            if(empty($package_info)){
                $this->ajaxReturn(['status'=>false,'info'=>'参数错误']);
            }

            $package_info['end_time'] = strtotime($package_info['end_time']);

            $result = apiCall(ProductPackageApi::PACKAGE_SAVE,[$package_info]);
            if($result['status']){
                $this->success($result['info']);
            }else{
                $this->error('保存失败');
            }

        }
    }

    private function getPackageInfo($package_id){
        //查询套餐详情
        $result = apiCall(ProductPackageApi::PACKAGE_DETAIL,[$package_id]);

        if($result['status']){
            if(!is_null($result['info'])){
                $info_items = $result['info']['items'];
                $main_info = $result['info']['main_info'];
                $package_info = [
                    'package_id' => $main_info['package_id'],
                    'package_img' => $main_info['package_img'],
                    'package_name' => $main_info['package_name'],
                    'create_time' => $main_info['create_time'],
                    'end_time' => $main_info['end_time'],
                    'package_price' => $main_info['package_price'],
                    'package_desc' => $main_info['package_desc'],
                    'dt_type' => $main_info['dt_type']
                ];
                $items = [];
                foreach($info_items as $val){
                    $items[] = [
                        'p_id' => $val['p_id'],
                        'is_main' => $val['is_main'],
                        'product_name' => $val['product_name'],
                        'sku_id' => $val['sku_id'],
                        'sku_desc' => $val['sku_desc'],
                        'img_id' => $val['img_id'],
                        'icon_url' => $val['icon_url']
                    ];
                }
                $package_info['items'] = $items;

                return ['status'=>true,'info'=>$package_info];
            }else{
                return ['status'=>false, 'info'=>'套餐不存在'];
            }
        }else{
            return ['status'=>false, 'info'=>$result['info']];
        }

    }



}